package logrus.beanmock.web;

import java.util.Arrays;
import java.util.LinkedList;

import javax.servlet.ServletRequest;

public class RequestParamScenarioExtractor implements ScenarioExtractor {

    public static final String DEFAULT_PARAM_NAME = "_scenario";

    private static final String[] EMPTY_ARRAY = new String[0];

    private final String paramName;

    public RequestParamScenarioExtractor() {
        this(DEFAULT_PARAM_NAME);
    }

    public RequestParamScenarioExtractor(String paramName) {
        this.paramName = paramName;
    }

    public String[] getScenarios(ServletRequest request) {
        String[] values = request.getParameterValues(paramName);
        if (values == null) {
            return EMPTY_ARRAY;
        }
        LinkedList<String> builder = new LinkedList<String>();
        for (String value : values) {
            String[] names = value.split(",");
            builder.addAll(Arrays.asList(names));
        }
        return builder.toArray(EMPTY_ARRAY);
    }

}
